package com.innovation.chengzhangriji.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.ConfigurationManager;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.innovation.library.util.InnovationAlgorithm;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.LoginResponse;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.animation.AnimationConfig;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteEditGroup;
import com.jiuan.oa.android.library.util.MobileNumberUtil;
import com.umeng.analytics.MobclickAgent;

public class LoginNormalFragment extends BaseLoginFragment implements View.OnClickListener {

    private CuteEditGroup mCegNumber;

    private CuteEditGroup mCegPassword;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login_normal, container, false);
        mCegNumber = (CuteEditGroup) view.findViewById(R.id.edit_text_number);
        mCegPassword = (CuteEditGroup) view.findViewById(R.id.edit_text_password);
        CuteTextView textView = (CuteTextView) view.findViewById(R.id.text_forget);
        textView.setOnClickListener(this);
        CuteButton button = (CuteButton) view.findViewById(R.id.btn_login);
        button.setOnClickListener(this);
        return view;
    }

    @Override
    protected void onPageStart() {
        MobclickAgent.onPageStart(Config.UMENG_PAGE_LOGIN_NORMAL);
    }

    @Override
    protected void onPageEnd() {
        MobclickAgent.onPageEnd(Config.UMENG_PAGE_LOGIN_NORMAL);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String number = mCegNumber.getContent();
                String password = mCegPassword.getContent();
                if (checkInput(number, password)) {
                    InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    manager.hideSoftInputFromWindow(mCegNumber.getWindowToken(), 0);
                    requestLogin(number, InnovationAlgorithm.SHA1(Config.getConfig(ConfigurationManager.PASSWORD_SALT), password));
                }
                break;
            case R.id.text_forget:
                Intent intent = new Intent(getActivity(), ForgetActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }

    private boolean checkInput(String number, String password) {
        if (!MobileNumberUtil.isMobile(number)) {
            AnimationConfig.shake(getActivity(), mCegNumber);
            tipShow(R.string.tip_error_number);
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            AnimationConfig.shake(getActivity(), mCegPassword);
            tipShow(R.string.tip_error_password_need);
            return false;
        }
        return true;
    }

    private void requestLogin(final String number, final String password) {
        Client.requestLogin(getActivity(), number, password, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                dismissProgressDialog();
                if (mLoginSuccessListener != null) {
                    LoginResponse response = get(value.toString(), LoginResponse.class);
                    if (response != null) {
                        LoginInfo login = new LoginInfo();
                        login.setUserID(response.getId());
                        login.setName(response.getUserName());
                        login.setPhone(number);
                        login.setGender(response.getGender());
                        login.setUrl(response.getUrl());
                        login.setToken(response.getToken());
                        mLoginSuccessListener.loginSuccess(login);
                    }
                }
            }

            @Override
            public void onChZhRJFailure(String value) {
                dismissProgressDialog();
                tipShow(value);
            }
        }, networkUnavailableSampleImpl());
    }
}
